clear
use ${data}preference_data.dta

* Exclude school outside options 
drop if outside_cm==1
 

* -------------------------------------------------- *
* Estimate rank ordered logit for school preferences *
* -------------------------------------------------- *
cmset schoolcert_id, noalternative
 
eststo clear

eststo sch:  cmrologit _rank_school    gpa_std  undergrad_satmt75_std   major_ed  major_mathScience major_humanities   major_other  doubleMajor  yearsSinceCollege_std  female white black hispanic otherRace  white_maj_black black_maj_black hisp_maj_black otherRace_black outside_int2 outside_int3 outside_stem outside_sped outside_int_b  undergrad_sat_ms , incomplete(0) altwise    cluster(school_id)
gen s=e(sample)
unique school_id if s==1
estadd scalar nsch = r(unique): sch
unique schoolcert_id if s==1
estadd scalar nschcert = r(unique): sch
drop s

estout  using ${results}table1_schoolpref.txt, replace ///
	cells(b(star fmt(2) nostar)  se(par fmt(2))) ///
	collabels(,none) stat(nsch nschcert N) 
	
	
	
	
	
* Bootstrap draws	
quietly {
forv i=0/$bootstraps {

	predict uhat_sch`i', xb


	gen _outside_option_bs`i' = .
		replace _outside_option_bs`i' =0 if intDay==1 & majority_black==0 & cert_group==1
		replace _outside_option_bs`i' =0 + _b[outside_stem]  if intDay==1 & majority_black==0 & cert_group==2
		replace _outside_option_bs`i' =0 + _b[outside_sped]  if intDay==1 & majority_black==0 & cert_group==3
	
		replace _outside_option_bs`i' =0+_b[outside_int_b] if intDay==1 & majority_black==1 & cert_group==1
		replace _outside_option_bs`i' =0 + _b[outside_stem] + _b[outside_int_b]  if intDay==1 & majority_black==1 & cert_group==2
		replace _outside_option_bs`i' =0 + _b[outside_sped] + _b[outside_int_b]  if intDay==1 & majority_black==1 & cert_group==3
	
	
		replace _outside_option_bs`i' =0 + _b[outside_int2] if intDay==2 & majority_black==0 & cert_group==1
		replace _outside_option_bs`i' =0 + _b[outside_stem] + _b[outside_int2]  if intDay==2 & majority_black==0 & cert_group==2
		replace _outside_option_bs`i' =0 + _b[outside_sped] + _b[outside_int2]  if intDay==2 & majority_black==0 & cert_group==3
	
		replace _outside_option_bs`i' =0 + _b[outside_int_b] + _b[outside_int2] if intDay==2 & majority_black==1 & cert_group==1
		replace _outside_option_bs`i' =0 + _b[outside_stem] + _b[outside_int_b]+_b[outside_int2]  if intDay==2 & majority_black==1 & cert_group==2
		replace _outside_option_bs`i' =0 + _b[outside_sped] + _b[outside_int_b]+_b[outside_int2]  if intDay==2 & majority_black==1 & cert_group==3
			
		replace _outside_option_bs`i' =0 + _b[outside_int3] if intDay==3 & majority_black==0 & cert_group==1
		replace _outside_option_bs`i' =0 + _b[outside_stem] + _b[outside_int3]  if intDay==3 & majority_black==0 & cert_group==2
		replace _outside_option_bs`i' =0 + _b[outside_sped] + _b[outside_int3]  if intDay==3 & majority_black==0 & cert_group==3
	
		replace _outside_option_bs`i' =0 + _b[outside_int_b] + _b[outside_int3] if intDay==3 & majority_black==1 & cert_group==1
		replace _outside_option_bs`i' =0 + _b[outside_stem] + _b[outside_int_b] + _b[outside_int3]  if intDay==3 & majority_black==1 & cert_group==2
		replace _outside_option_bs`i' =0 + _b[outside_sped] + _b[outside_int_b] + _b[outside_int3]  if intDay==3 & majority_black==1 & cert_group==3
		
		preserve	
		bsample, cluster(schoolcert_id)
		cmrologit _rank_school   outside_int2 outside_int3 outside_stem outside_sped outside_int_b  gpa_std  undergrad_satmt75_std   major_ed  major_mathScience major_humanities   major_other  doubleMajor  yearsSinceCollege_std  female white black hispanic otherRace  white_maj_black black_maj_black hisp_maj_black otherRace_black   undergrad_sat_ms , incomplete(0) altwise    cluster(school_id) 
		restore 

}
}


keep if outside_sch==0

keep schoolcert_id teacher_id uhat_sch* _outside_option_bs* 
save ${data}Simulation/school_uhats.dta, replace


exit


